package com.zhengshi.formal;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.channels.FileChannel;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.InputFormatException;

public class 过滤视频20180209 {
	// 去除下载重复的视频
	public static void main(String[] args) {
		// ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆唯一更改◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆
		String filepath = "E:\\宠物\\视频";//
		// ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆唯一更改◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆

		过滤视频20180209 cleanDemo = new 过滤视频20180209();
		try {
			cleanDemo.doClean(filepath);
		} catch (Exception e) {
			try {
				cleanDemo.doClean(filepath);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}

	public void doClean(String filepath) throws Exception {
		// **************生成随机视频文件列表列表****************
		File shiPingAExist = new File(filepath + "/顺序.txt");// 读视频文件
		if (!shiPingAExist.exists()) {

			// **************生成随机视频文件列表列表****************
			String filepath1 = filepath;
			File file = new File(filepath);
			if (file.isDirectory()) {
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++) {
					System.gc();
					System.out.println("循环" + (i + 1) + "/" + filelist.length);
					String fileName = filelist[i];
					File fileDel = new File(filepath1 + "/" + fileName);
					if (fileName.endsWith(".txt")) {
						continue;
					}

					if (fileName.length() < 5) {
						System.gc();
						fileDel.delete();
						System.out.println("视频为空标题的直接删除");
						continue;
					}

					String endWith123 = fileName.split(".mp4")[0];
					System.out.println(fileName);

					String str123 = new String(endWith123);
					str123 = endWith123.substring(endWith123.length() - 1, endWith123.length());
					if (isNum(str123)) {
						System.gc();
						fileDel.delete();
						System.out.println("末位为数字的视频删除成功");
						continue;
					}
					
					if (endingChar(str123)) {
						System.gc();
						fileDel.delete();
						System.out.println("末位为字母的视频删除成功");
						continue;
					}

					if (endWith123.endsWith(")")) {
						System.gc();
						fileDel.delete();
						System.out.println("重复下载的视频删除成功");
						continue;
					}
					if (endWith123.endsWith("）")) {
						System.gc();
						fileDel.delete();
						System.out.println("恶意加符号  ）的视频删除成功");
						continue;
					}
					if (endWith123.length() < 11 || endWith123.length() > 50) {
						System.gc();
						fileDel.delete();
						System.out.println("文件名小于11大于50删除成功");
						continue;
					}
					if (fileName.endsWith(".vd!")) {
						System.gc();
						fileDel.delete();
						System.out.println("维棠未下载完全的删除成功");
						continue;
					}
					if (fileDel.length() > 99999999 || fileDel.length() < 300000) {
						System.gc();
						fileDel.delete();
						System.out.println("文件过大或者过小，删除成功");
						continue;
					}
					if (fileName.endsWith(".ts") || fileName.endsWith(".f4v")) {
						File fileReName = new File(filepath1 + "/" + fileName);
						File mm = new File(filepath1 + "/" + endWith123.split(".f4v")[0].split(".ts")[0] + ".mp4");
						fileReName.renameTo(mm);
						System.out.println("文件改名成功");
						filelist[i] = endWith123.split(".f4v")[0].split(".ts")[0] + ".mp4";
						i--;
						continue;
					}

					try {
						File source = new File(filepath1 + "/" + fileName);
						if (source.exists()) {
							Encoder encoder = new Encoder();
							FileChannel fc = null;
							String size = "";

							it.sauronsoftware.jave.MultimediaInfo m = encoder.getInfo(source);
							long ls = m.getDuration();
							System.out.println("视频名称为:" + fileName);
							System.out.println("此视频时长为:" + ls / 60000 + "分" + (ls) % 60000 / 1000 + "秒！");
							// 视频帧宽高
							if (m.getVideo() != null) {
								System.out.println("此视频高度为:" + m.getVideo().getSize().getHeight());
								System.out.println("此视频宽度为:" + m.getVideo().getSize().getWidth());
								if (m.getVideo().getSize().getHeight() < 320
										|| m.getVideo().getSize().getWidth() < 320) {
									System.gc();
									fileDel.delete();
									System.out.println("视频帧高度或者宽度低于320，删除成功");
									continue;
								}
							} else {
								System.gc();
								fileDel.delete();
								System.out.println("视频帧高度和宽度读不出来，删除成功");
								continue;
							}
							String videoFormat = m.getFormat();
							System.out.println("此视频格式为:" + videoFormat);
							if (!"mov".equals(videoFormat)) {
								System.gc();
								fileDel.delete();
								System.out.println("非视频格式的要删除，删除成功");
								continue;
							}
							@SuppressWarnings("resource")
							FileInputStream fis = new FileInputStream(source);
							fc = fis.getChannel();
							BigDecimal fileSize = new BigDecimal(fc.size());
							size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB";
							System.out.println("此视频大小为" + size);
						}
					} catch (InputFormatException e) {
						System.gc();
						fileDel.delete();
						System.out.println("视频播放不出来，文件已经损坏，直接删除");
						e.printStackTrace();
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (EncoderException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				System.out.println("所有视频处理完毕");
			}
		}
	}

	// 1.判断一个字符串的末位字符是否为字母
	public static boolean endingChar(String str) {
		String cString = str.substring(str.length() - 1, str.length());
		char[] cChar = cString.toCharArray();
		int i = (int) cChar[0];
		if ((i >= 65 && i <= 90) || (i >= 97 && i <= 122)) {
			return true;
		} else {
			return false;
		}
	}

	// 2.判断一个字符串的首字符是否为字母
	public static boolean frontChar(String s) {
		char c = s.charAt(0);
		int i = (int) c;
		if ((i >= 65 && i <= 90) || (i >= 97 && i <= 122)) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean check(String fstrData) {
		char c = fstrData.charAt(0);
		if (((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) {
			return true;
		} else {
			return false;
		}
	}

	// 3 .判断是否为汉字
	public boolean vd(String str) {

		char[] chars = str.toCharArray();
		boolean isGB2312 = false;
		for (int i = 0; i < chars.length; i++) {
			byte[] bytes = ("" + chars[i]).getBytes();
			if (bytes.length == 2) {
				int[] ints = new int[2];
				ints[0] = bytes[0] & 0xff;
				ints[1] = bytes[1] & 0xff;

				if (ints[0] >= 0x81 && ints[0] <= 0xFE && ints[1] >= 0x40 && ints[1] <= 0xFE) {
					isGB2312 = true;
					break;
				}
			}
		}
		return isGB2312;
	}

	public static boolean isNum(String str) {
		return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
	}

}
